﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;
using Ext.Net;

namespace SalesManagement.Controls.List
{
    public partial class CountryForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string countryId)
        {
            CountryEntity countryE = new CountryEntity();
            CountryService countryS = CountryService.GetInstance();
            countryE.CountryId = int.Parse(countryId);
            countryE = countryS.LoadByPrimaryKey(countryE);
            if (null == countryE)
                return;
            hdfCountryId.Value = countryE.CountryId;
            txtCountryName.Text = countryE.CountryName;
            txtDescription.Text = countryE.Description;
            ckbIsActive.Checked = countryE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            CountryEntity countryE = new CountryEntity();
            CountryService countryS = CountryService.GetInstance();
            if ("" != hdfCountryId.Value.ToString())
            {
                string countryId = hdfCountryId.Value.ToString();
                countryE.CountryId = int.Parse(countryId);
                countryE = countryS.LoadByPrimaryKey(countryE);
                countryE.CountryName = txtCountryName.Text;
                countryE.Description = txtDescription.Text;
                countryE.IsActive = ckbIsActive.Checked;
                countryE.UpdatedDate = DateTime.Now;
                countryE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                countryS.Update(countryE);
            }
            else
            {
                countryE.CountryName = txtCountryName.Text;
                countryE.Description = txtDescription.Text;
                countryE.IsActive = ckbIsActive.Checked;
                countryE.CreatedDate = DateTime.Now;
                countryE.CreatedBy = FSSContext.Current.User.EmployeeId;
                countryE.UpdatedDate = DateTime.Now;
                countryE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                countryS.Insert(countryE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}